<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
// use think\facade\Session;
use app\common\model\SystemAdmin;
use app\common\model\User;
use app\common\model\Notepad;
use app\common\controller\Handle;
use app\common\model\Store;

class ApiNotepad extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
        $this->user = new User;
        $this->notepad = new Notepad;
        $this->Store = new Store;
    }

    // 添加记事本
    public function addNotepad()
    {
        $data = request()->post();

        if(empty($data['content'])){
            return json(['status' => 0, 'msg' =>"请上传记录内容"]);
        }

        if(empty($data['title'])){
            return json(['status' => 0, 'msg' =>"请上传记录标题"]);
        }

        $add['content'] = $data['content'];
        $add['title'] = $data['title'];
        $add['user_id'] = $this->uid;
        $add['created_at'] = date("Y-m-d H:i:s");
        $add['update_at'] = date("Y-m-d H:i:s");

        $res = $this->notepad->create($add);

        return json(['status'=> 1,'msg'=> '操作成功']);
    }

    // 记事本列表
    public function notepadList()
    {
        $data = request()->post();

        if(empty($data['limit'])){
            $data['limit'] = 10;
        }

        if(empty($data['page'])){
            $data['page'] = 1;
        }

        $list = $this->notepad->getPagelist(['user_id'=>$this->uid,"deleted_at"=>null],"id,content,title,created_at,user_id","created_at desc");

        $count = $this->notepad->countRows(['user_id'=>$this->uid,"deleted_at"=>null]);

        $pages = ceil($count/$data['limit']);

        return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);
    }


    // 查看记事本
    public function getNotepad()
    {
        $data = request()->post();

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传记录id"]);
        }

        $list  = $this->notepad->getOne(['id'=> $data['id'],"deleted_at"=>null]);
        if(empty($list)){
            return json(['status' => 0, 'msg' =>"记录不存在"]);
        }

        return json(['status'=> 1,'data'=>$list]);
    }

    // 修改记事本
    public function updateNotepad()
    {
        $data = request()->post();

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传记录id"]);
        }

        $list  = $this->notepad->getOne(['id'=> $data['id'],"deleted_at"=>null]);
        if(empty($list)){
            return json(['status' => 0, 'msg' =>"记录不存在"]);
        }

        if(empty($data['content'])){
            return json(['status' => 0, 'msg' =>"请上传记录内容"]);
        }

        if(empty($data['title'])){
            return json(['status' => 0, 'msg' =>"请上传记录标题"]);
        }

        $save['content'] = $data['content'];
        $save['update_at'] =  date("Y-m-d H:i:s");
        $save['title'] =  $data['title'];

        $res = $this->notepad->where(['id'=>$data['id']])->update($save);

        return json(['status'=> 1,'msg'=> '操作成功']);

    }

    // 删除记事本
    public function delNotepad()
    {
        $data = request()->post();

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传记录id"]);
        }

        $list  = $this->notepad->getOne(['id'=> $data['id'],"deleted_at"=>null]);
        if(empty($list)){
            return json(['status' => 0, 'msg' =>"记录不存在"]);
        }

        $save['deleted_at'] =  date("Y-m-d H:i:s");

        $res = $this->notepad->where(['id'=>$data['id']])->update($save);

        return json(['status'=> 1,'msg'=> '操作成功']); 
    }
    
}
